package com.holdtime.jtt809.taxi.module.service.impl;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import com.holdtime.jtt809.taxi.module.entity.PlatformLogoutLogDO;
import com.holdtime.jtt809.taxi.module.entity.mapper.PlatformLogoutLogMapper;
import com.holdtime.jtt809.taxi.module.model.vo.PlatformLogoutLogPageReqVO;
import com.holdtime.jtt809.taxi.module.model.vo.PlatformLogoutLogSaveReqVO;
import com.holdtime.jtt809.taxi.module.service.PlatformLogoutLogService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.holdtime.jtt809.taxi.module.enums.Taxi809ErrorCodeConstants.PLATFORM_LOGOUT_LOG_NOT_EXISTS;

/**
 * 下级平台登出日志 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class PlatformLogoutLogServiceImpl implements PlatformLogoutLogService {

    @Resource
    private PlatformLogoutLogMapper platformLogoutLogMapper;

    @Override
    public String createPlatformLogoutLog(PlatformLogoutLogSaveReqVO createReqVO) {
        // 插入
        PlatformLogoutLogDO platformLogoutLog = BeanUtils.toBean(createReqVO, PlatformLogoutLogDO.class);
        platformLogoutLogMapper.insert(platformLogoutLog);
        // 返回
        return platformLogoutLog.getId();
    }

    @Override
    public void updatePlatformLogoutLog(PlatformLogoutLogSaveReqVO updateReqVO) {
        // 校验存在
        validatePlatformLogoutLogExists(updateReqVO.getId());
        // 更新
        PlatformLogoutLogDO updateObj = BeanUtils.toBean(updateReqVO, PlatformLogoutLogDO.class);
        platformLogoutLogMapper.updateById(updateObj);
    }

    @Override
    public void deletePlatformLogoutLog(String id) {
        // 校验存在
        validatePlatformLogoutLogExists(id);
        // 删除
        platformLogoutLogMapper.deleteById(id);
    }

    private void validatePlatformLogoutLogExists(String id) {
        if (platformLogoutLogMapper.selectById(id) == null) {
            throw exception(PLATFORM_LOGOUT_LOG_NOT_EXISTS);
        }
    }

    @Override
    public PlatformLogoutLogDO getPlatformLogoutLog(String id) {
        return platformLogoutLogMapper.selectById(id);
    }

    @Override
    public PageResult<PlatformLogoutLogDO> getPlatformLogoutLogPage(PlatformLogoutLogPageReqVO pageReqVO) {
        return platformLogoutLogMapper.selectPage(pageReqVO);
    }

}